<template>
  <div id="subpage">
    <el-col :span="24" class="warp-breadcrum">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <b>城市管理</b>
        </el-breadcrumb-item>
        <el-breadcrumb-item>城市列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
    <div class="content">
      <div class="xcx-head">
        <div style="display: flex">
          <span>城市列表</span>
        </div>
        <div style="display: flex">
          <el-button @click="dialogFormVisible = true" size="small" icon="el-icon-plus" type="success">添加城市</el-button>
        </div>
      </div>

      <div class="xcx-content">
        <!--列表-->
        <el-tree :data="tableData" node-key="id" default-expand-all :expand-on-click-node="false">
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <span>{{ data.title }}</span>
            <span>
              <el-button plain icon="el-icon-edit" size="mini" type="primary" @click="edit(data)">编辑</el-button>
              <el-button plain icon="el-icon-delete" size="mini" type="danger" @click="del_item(data)">删除</el-button>
            </span>
          </span>
        </el-tree>
      </div>
    </div>

    <el-dialog title="新增城市" width="36%" :visible.sync="dialogFormVisible" :close-on-click-modal="false"
      @close="closeDialog">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item label="归属：" :label-width="formLabelWidth" prop="par_id">
          <el-select style="width: 100%;" v-model="ruleForm.par_id" placeholder="请选择">
            <el-option label="请选择" :value="0" placeholder="顶级省份">
            </el-option>
            <el-option v-for="item in tableData" :key="item.id" :label="item.title" :value="item.id" placeholder="顶级归属">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称：" :label-width="formLabelWidth" prop="title">
          <el-input v-model="ruleForm.title" placeholder="请输入会员卡名称" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序：" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.sort" placeholder="请输入排序数字（由小到大排序）" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="width: 120px;" icon="el-icon-back" @click="dialogFormVisible = false">取 消</el-button>
        <el-button style="width: 120px;" icon="el-icon-thumb" type="primary" @click="submitForm('ruleForm')">确
          定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: true,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      tableData: [],
      ruleForm: {
        id: 0,
        title: "",
        par_id: "",
        sort: 0,
      },
      rules: {
        title: [{ required: true, message: "请填写名称", trigger: "blur" }],
      },
    };
  },
  //进入页面加载
  mounted: function () {
    var that = this;
    that.getList();
  },
  methods: {

    closeDialog() {
      this.ruleForm.title = "";
      this.ruleForm.id = 0;
      this.ruleForm.sort = 0;
      this.ruleForm.column_ary = [];
      this.dialogFormVisible = false;
    },

    edit(data) {
      var that = this;
      that.id = data.id;
      that.axios.post("/area/detail", { id: data.id }).then((res) => {
        if (res) {
          that.ruleForm.title = res.data.title;
          that.ruleForm.sort = res.data.sort;
          that.ruleForm.id = res.data.id;
          that.ruleForm.par_id = res.data.par_id;
          that.dialogFormVisible = true;
        }
      });
    },

    del_item(data) {
      let that = this;
      this.$confirm("此操作不可逆, 是否继续?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        that.axios
          .post("/area/destroy", {
            ids: data.id,
          })
          .then((res) => {
            console.log(res);
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              that.getList();
            } else {
              this.$message({
                type: "info",
                message: res.msg,
              });
            }
          });
      });
    },

    /**
     * 获取列表
     */
    getList() {
      let that = this;
      let formData = that.search;
      that.axios.post("/area/index", formData).then((res) => {
        if (res) {
          that.tableData = res.data;
          that.loading = false;
        }
      });
    },

    submitForm(formName) {
      const that = this;
      that.$refs[formName].validate((valid) => {
        if (!valid) return that.$message.warning("请完整填写内容!");
        var formData = {};
        formData = that.ruleForm;
        var url = "/area/beforeSave";
        that.axios.post(url, formData).then(() => {
          that.$message.success(that.id ? "修改成功" : "添加成功");
          that.dialogFormVisible = false;
          that.ruleForm.title = "";
          that.ruleForm.id = 0;
          that.ruleForm.sort = 0;
          that.ruleForm.column_ary = [];
          that.getList();
        });
      });
    },
  },
};
</script>
<style>
.xcx-content {
  border-top: 1px solid #eee;
}

.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}

.el-tree-node__content {
  height: 36px;
  line-height: 36px;
}
</style>